<template>
	<view class="container">
		<!-- 页面内容区域 -->
		<view class="content">
			<view class="address-list">
				<view v-for="(item, index) in addressList" :key="item.id" class="address-item"
					@click="selectAddress(item)">
					<view class="address-info">
						<view class="contact">
							<text class="name">{{ item.contactName }}</text>
							<text class="phone">{{ item.phone }}</text>
						</view>
						<view class="address">
							<text>{{ item.fullAddress }}{{ item.detailAddress }}</text>
						</view>
					</view>
					<view class="address-actions">
						<text class="edit-icon" @click.stop="editAddress(item)">修改✏️</text>
						<text class="delete-icon" @click.stop="deleteAddress(item.id)">删除×️</text>
					</view>
				</view>

				<view v-if="addressList.length === 0" class="empty-tip">
					<text>暂无地址，请添加新地址</text>
				</view>
			</view>
		</view>

		<!-- 固定在底部的按钮 -->
		<view class="bottom-button">
			<button class="add-address-btn" @click="navigateToAddAddress">新增地址</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				isSelectMode: false,
				// address: [],
				// detailAddress: [],
				// contactName: [],
				// phone: []
				param: {
					address: "海淀区",
					details: "张江高科技园区",
					contact_name: "黄宇翔",
					contact_phone: "18784404870"
				}
			}
		},
		onLoad(options) {
			// 判断是否是从其他页面跳转过来选择地址
			if (options.select === 'true') {
				this.isSelectMode = true;
			}
		},
		onShow() {

			this.loadAddressList();
		},
		methods: {
			loadAddressList() {
				this.addressList = uni.getStorageSync('addressList') || [];
				this.token = uni.getStorageSync('token');
				this.$http.get("https://billiards.cqtrust.cn/addresses", this.param)
					.then((res) => {
						console.log(res);
						const {
							code,
							msg,
							data
						} = res;
						uni.showToast({
							title: msg,
							icon: 'none'
						});
						if (code === 1 || code === 200) {
							if (data && data.length > 0) {
								this.loadAddressList = data;
								console.log(data);

							}
							return Promise.resolve(true);
						}
						return Promise.reject(false);
					})
					.catch((res) => {
						console.log(res);
						return Promise.reject(false);
					});
			},


			navigateToAddAddress() {
				uni.navigateTo({
					url: '/pages/my/address/index2/index2',
					success: () => {
						console.log('跳转到新增地址页面成功');
					},
					fail: (err) => {
						console.error('跳转失败:', err);
						uni.showToast({
							title: '跳转失败，请重试',
							icon: 'none'
						});
					}
				});
			},
			editAddress(item) {


				uni.navigateTo({
					url: `/pages/my/address/index2/index2?id=${item.id}`
				});
			},
			deleteAddress(id) {
				uni.showModal({
					title: '提示',
					content: '确定要删除该地址吗？',
					success: (res) => {
						if (res.confirm) {
							this.addressList = this.addressList.filter(item => item.id !== id);
							uni.setStorageSync('addressList', this.addressList);
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
						}
					}
				});
			},
			selectAddress(item) {
				if (this.isSelectMode) {
					try {

						const eventChannel = this.getOpenerEventChannel();

						eventChannel.emit('acceptAddress', {
							id: item.id,
							fullAddress: item.fullAddress,
							detailAddress: item.detailAddress,
							contactName: item.contactName,
							phone: item.phone
						});


						uni.navigateBack({
							delta: 1
						});
					} catch (error) {
						console.error('地址选择失败:', error);
						uni.showToast({
							title: '地址选择失败，请重试',
							icon: 'none'
						});
					}
				} else {

					const pages = getCurrentPages();

					const prevPage = pages[pages.length - 2];

					if (prevPage && prevPage.selectAddress) {
						prevPage.selectAddress(item);
						uni.navigateBack();
					} else {
						uni.showToast({
							title: `已选择地址: ${item.contactName} ${item.phone}`,
							icon: 'none'
						});

						uni.setStorageSync('selectedAddress', item);
					}
				}
			}



		}
	}
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-bottom: 120rpx;
	}

	.content {
		padding: 20rpx;
	}

	.address-list {
		.address-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #fff;
			padding: 30rpx;
			margin-bottom: 20rpx;
			border-radius: 12rpx;

			.address-info {
				flex: 1;

				.contact {
					display: flex;
					align-items: center;
					margin-bottom: 15rpx;

					.name {
						font-size: 32rpx;
						font-weight: bold;
						color: #333;
						margin-right: 30rpx;
					}

					.phone {
						font-size: 28rpx;
						color: #666;
					}
				}

				.address {
					font-size: 28rpx;
					color: #666;
					line-height: 1.5;
				}
			}

			.address-actions {
				margin-left: 30rpx;

				.edit-icon {
					font-size: 36rpx;
					color: #fdad18;
				}
			}
		}

		.empty-tip {
			text-align: center;
			padding: 100rpx 0;
			color: #999;
			font-size: 28rpx;
		}
	}

	.bottom-button {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		background-color: #f5f5f5;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.add-address-btn {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #fdad18;
		color: #fff;
		border: none;
		border-radius: 40rpx;
		font-size: 32rpx;
		font-weight: bold;

		&:active {
			opacity: 0.9;
		}
	}
</style>